import { ref } from 'vue'
import { defineStore } from 'pinia'
import {
  searchProvince,
  searchCity,
  searchCounty,
  searchWay,
  searchAddress,
  searchAddressByUserId,
  updateAddress,
  insertAddress,
  deleteAddress
} from '@/api/address'
import {
  province,
  city,
  county,
  way,
  address,
  userAddress
} from '@/model/addressModel'
export default defineStore('address', () => {
  const addressArr = ref({
    id: 0,
    name: '',
    phone: '',
    detail: '',
    province: '北京',
    city: '北京市',
    county: '海淀区',
    way: '清河街道'
  })
  const saveAddress = (address: any) => {
    addressArr.value = { ...addressArr.value, ...address }
    localStorage.getItem('userId') &&
      (userAddress.value = {
        ...userAddress.value!,
        ...{
          province_name: address.province,
          city_name: address.city,
          county_name: address.county,
          way_name: address.way
        }
      })
  }
  const provinceList = ref<province[]>()
  const cityList = ref<city[]>()
  const countyList = ref<county[]>()
  const wayList = ref<way[]>()
  const addressList = ref<address[]>([])
  const userAddressList = ref<userAddress[]>([])
  const addressPage = ref<number>(1)

  const searchProvinceList = async () => {
    const result = await searchProvince()
    provinceList.value = result.data
  }
  const searchCityList = async (provinceId: number) => {
    const result = await searchCity(provinceId)
    cityList.value = result.data
  }
  const searchCountyList = async (cityId: number) => {
    const result = await searchCounty(cityId)
    countyList.value = result.data
  }
  const searchWayList = async (countyId: number) => {
    const result = await searchWay(countyId)
    wayList.value = result.data
  }
  const searchAddressList = async (wayName: string) => {
    const result = await searchAddress(wayName)
    addressList.value = result.data
  }
  const searchAddressListByUserId = async (
    userId: number,
    currentPage: number,
    pageSize: number = 4
  ) => {
    const result = await searchAddressByUserId(userId, currentPage, pageSize)
    result.status === 200 && (userAddressList.value = result.data)
    addressPage.value = Math.ceil((result as any).length / pageSize)
  }
  const userAddress = ref<userAddress>()
  const updAddress = async (address: userAddress) => {
    const result = await updateAddress(address)
    if (result.status === 200)
      searchAddressListByUserId(Number(localStorage.getItem('userId')), 1, 9999)
  }
  const insAddress = async (address: userAddress, user_id: number) => {
    const result = await insertAddress(address, user_id)
    if (result.status === 200)
      searchAddressListByUserId(Number(localStorage.getItem('userId')), 1, 9999)
  }
  const delAddress = async (address_id: number) => {
    const result = await deleteAddress(address_id)
    result.status === 200 &&
      searchAddressListByUserId(Number(localStorage.getItem('userId')), 1, 9999)
  }
  return {
    addressArr,
    saveAddress,
    provinceList,
    cityList,
    countyList,
    wayList,
    addressList,
    userAddressList,
    addressPage,
    searchProvinceList,
    searchCityList,
    searchCountyList,
    searchWayList,
    searchAddressList,
    searchAddressListByUserId,
    userAddress,
    updAddress,
    insAddress,
    delAddress
  }
})
